<?php
/**
 * Created by PhpStorm.
 * User: thanatos
 * Date: 2017/9/14
 * Time: 下午8:54
 */

namespace backend\components;


use yii\base\Component;
use Yii;

/**
 * 处理菜单
 *
 * @package backend\components
 */
class MenuComponent extends Component
{
    public $menu;
    
    public static function prepare($menu)
    {
        foreach($menu as $k => $v){
            foreach($v['items'] as $key=>$value){
                $route = $value['url'][0];
                if ($route[0] !== '/' && Yii::$app->controller) {
                    $route = ltrim(Yii::$app->controller->module->getUniqueId() . '/' . $route, '/');
                }
                $route = ltrim($route , '/');
                $route_1 = explode('/', $route);
                $route_2 = explode('/', Yii::$app->controller->route);
                if( $route_1[0] == $route_2[0] ){
                    $menu[$k]['items'][$key]['active'] = true;
                }
            }
        }
        return $menu;
    }
    
}